package com.set;

public class MyHashSet implements MyHash {
    public int size;

    public static class Node{
        public int key;
        public int val;
        Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    public  Node[] arrary=new Node[10];
    public static double LOAD_FACTOR=0.75;

    @Override
    public void put(int key, int val) {
        int index=key%arrary.length;
        Node cur=arrary[index];
        while(cur!=null){//如果元素相同则更新
            if(cur.key==key){
                cur.val=val;
                return;
            }
            cur=cur.next;
        }
        //没有重复元素则添加
        Node newNode=new Node(key,val);
        newNode.next=arrary[index];
        arrary[index]=newNode;
        size++;
        //超过0.75size扩容array
        if(doLoadFactor()>=LOAD_FACTOR){
            resize();
        }

    }
    public  double doLoadFactor(){
        return size*1.0/arrary.length;
    }
    public void resize(){
        Node[]newArray=new Node[arrary.length*2];
        for(int i=0;i<arrary.length;i++){
            Node cur=arrary[i];
            while(cur!=null){
               int index= cur.key% newArray.length;//新下标
               Node curN=cur.next;
               cur.next=newArray[index];
               newArray[index]=cur;
               cur=curN;
            }
        }
        arrary=newArray;
    }
}
